package it.trekker.upload;

import java.io.File;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.content.Context;
import android.util.Log;

public class UploadHelperRest extends UploadHelper {

	public UploadHelperRest(Context ctx) {
		super(ctx);
	}

	@Override
	public boolean uploadFile(String filePath, String serverUrl) {
		File f = new File(filePath);

		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(serverUrl);

		try {
			FileEntity entity = new FileEntity(f, "application/json");
			httppost.setEntity(entity);
			HttpResponse response = httpclient.execute(httppost);

			if(response.getStatusLine().getStatusCode() == 200)
				return true;
		}
		catch (ClientProtocolException e) {
			Log.d(TAG, e.getMessage(),e);
		}
		catch (IOException e) {
			Log.d(TAG, e.getMessage(),e);
		}

		return false;
	}
}
